<?php
/**
 * ExtBasicFormBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
class ExtBasicFormBase extends ExtObservable
{
    private $_baseparams_class_type = array('ExtParameter');

    public function parseNode(ExtHTMLParserNode $node,
        ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);
        $this->parseController($node, $astwalker);

        $this->parseParameters($node,$astwalker,
            'baseparameters',
            'baseParams',
            $this->_baseparams_class_type);
    }

    private function parseController($node,$astwalker)
    {
        $controller = str_ireplace('controller', '',trim($this->getControllerName()));
        if($controller != "")
        {
            $cmds = array('load','submit');
            $api = new ExtJavaScriptObject();

            foreach($cmds as $item)
                $api->addString($item, Yii::app()->urlManager->createUrl($controller.'/_'.$item));
            
            $this->setJavaScriptConfig('_ext4yii_basic_form_beforeaction_handler',
                sprintf('function(form,action){return ext4yii.prepareFormController(form,action,%s); }',$api->render()));

            $this->getController()->addAfterInitScript(
                sprintf('Ext.getCmp("%s").on("beforeaction",Ext.getCmp("%s")._ext4yii_basic_form_beforeaction_handler)',
                    $this->parent->getId(),$this->parent->getId()));

            $this->getController()->addAfterInitScript(
                sprintf('Ext.getCmp("%s").on("actionfailed",ext4yii.formControllerFailureHandler)',
                    $this->parent->getId(),$this->parent->getId()));
        }

    }


    public function renderConfig($asobject=true)
    {
        $msgtarget = null;
        if($this->parent instanceof ExtFormPanel)
        {
            /* do this anyway...*/
            $this->deleteConfig('controllername');

            $test = trim(strtolower($this->getWaitMsgTarget()));

            if(is_bool($this->getWaitMsgTarget()))
                $msgtarget = $this->getWaitMsgTarget();
            else if($test == 'body')
                $msgtarget = $this->JSImply ('Ext.getBody()');
            
            $this->setConfig('waitMsgTarget', $msgtarget);
            $this->parent->mergeConfigs($this->getAllConfigs());
            $this->parent->mergeMappings($this->configmap);
            $this->deleteAllConfigs();
        }
    }
}
?>
